<?php

require_once ('system/application/controllers/BaseController.php');
require_once ('system/application/models/NewsUtils.php');
require_once ('system/application/models/NewsModel.php');

class Admin extends BaseController {
	
	/**
	 * 
	 */
	function __construct() {
		parent::__construct ();
	
	}

	/**
	 * Default page - dispatch.<br />
	 * If the user is logged in go the control panel, otherwise show the login page.
	 *
	 */
	public function index() {
		if ($this->isLoggedIn()) {
			redirect("./admin/controlPanel");
		} else {
			$contentData['method'] = "admin.login";
			$this->viewPage("admin.login", $contentData, "path of decay - admin page");
		}
	}
		

	public function controlPanel() {
		if ($this->isLoggedIn()) {
			$contentData['method'] = "admin.controlPanel";
			$this->viewPage("adminControlPanel", $contentData, "path of decay - control panel page", "admin");
		} else {
			 redirect("./admin", 'refresh');
		}
		
	}
	
	public function postNews($newsID = null) {
		// show the form
		if ($this->isLoggedIn()) {
			$contentData['method'] = "admin.postNews";
			$contentData['langs'] = $this->getAllLangs();
			
			// default action is addNews
			$formAction = "addNews";
			$news = new NewsModel();
			if ($newsID != null) {
				// if $newsID is set that means we have open this page to edit this news!
				$news = new NewsModel($newsID);
				$formAction = "editNews";
			} 
			$contentData['news'] = $news;
			$contentData['formAction'] = $formAction;
			$this->viewPage("adminPostNews", $contentData, "path of decay - control panel - post news", "admin");
		} else {
			redirect("./admin", 'refresh');
		}
		
	}
	
	public function addNews() {
		// adds to the database
		
		if ($this->isLoggedIn()) {
			$langID = null;
			$title = null;
			$text = null;
			
			if (isset($_POST['langID'])){
				$langID = $_POST['langID'];
			}
			if (isset($_POST['title'])){
				$title = $_POST['title'];
			}
			if (isset($_POST['text'])) {
				$text = $_POST['text'];
			}

			// title
			$data = array(
				'content' => $title,
				'langID' => $langID
			);
			
			$this->db->insert('messages', $data);
			$titleID = $this->db->insert_id();
			
			
			// text
			$data = array(
				'content' => $text,
				'langID' => $langID
			);
			
			$this->db->insert('messages', $data);
			$textID = $this->db->insert_id();
			
			// news table
			$data = array(
				'titleMsgID' => $titleID,
				'textMsgID' => $textID,
				'langID' => $langID,
				'userID' => $this->getUser()->getUserID()
			);
			
			$this->db->insert('news', $data);
			$titleID = $this->db->insert_id();
			
			// go to the control panel
			 redirect("./admin/listNews", "refresh");
		} else {
			redirect("./admin" , "refresh");
		}
	}

	public function listNews() {
		if ($this->isLoggedIn()) {
			$langs = $this->getAllLangs();
			$allNews = array();
			try {
				foreach ($langs as $lang) {
					$nnews = NewsUtils::getInstance()->getNews(0, 
							$this->getParameters()->getNewsPerPage(), 
							$lang->getLangID());
					$allNews[$lang->getName()] = $nnews;
				}
				
				$contentData['langs'] = $langs;
				$contentData['allNews'] = $allNews;
				$contentData['method'] = "admin.listNews";
				$this->viewPage("adminListNews", $contentData, "path of decay - control panel - list news", "admin");
			} catch (Exception $e) {
				$this->handleException($e);
			}
		} else {
			redirect("./admin", 'refresh');
		}
	}
	
	public function deleteNews($newsID) {
		// check to se if user is logged in
		if ($this->isLoggedIn() == true) {
			$news = new NewsModel($newsID);
			if ($news != null && $news->getNewsID() > 0) {
				// delete the newsID entry
				try {
					$news->deleteFromDatabase();
				} catch (Exception $e) {
					// cannot delete, deal with that exception!
					// TODO: show an eror message!
					echo $e->getMessage();
				}
				redirect("./admin/listNews");
			} else {
				echo "not ok";
			}
			
		} else {
			// not logged in
			redirect("./admin/controlPanel");
		}
	}

	public function confirmDeleteNews($newsID) {
		$news = new NewsModel($newsID);
		echo "Are you sure you want to delete news entitled '<b>".$news->getTitle()."</b>'?";
		echo "<br /><br /><a href='deleteNews/$newsID'>Yes!</a>";
	}
	public function editNews() {
		$newTitle = $_POST['title'];
		$newText = $_POST['text'];
		$newLangID = $_POST['langID'];
		$newsID = $_POST['newsID'];
		
		$news = new NewsModel($newsID);
		$news->setText($newText);
		$news->setTitle($newTitle);
		$news->setLangID($newLangID);
		
		$news->updateDatabase();
		redirect("./admin/listNews", "refresh");
	}
}

?>
